﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectChords.Services.API;

namespace ProjectChords.Services.Impl.Memory
{
    /// <summary>
    /// This class provides CRUD (CReate, Update, Delete) functionality using an In-Memory implementation
    /// </summary>
    /// <typeparam name="T">Generic type T of the objects</typeparam>
    /// <see cref="IGenericCRUDService"/>
    public class GenericCRUDServiceInMemory<T> : IGenericCRUDService<T>
    {
        private Dictionary<string, T> IdToObjectDictionary { get; set; }

        public GenericCRUDServiceInMemory()
        {
            this.IdToObjectDictionary = new Dictionary<string, T>();
        }

        public T LoadFromId(string id)
        {
            return this.IdToObjectDictionary[id];
        }

        public void Add(string id, T obj)
        {
            this.IdToObjectDictionary.Add(id, obj);
        }

        public void Update(string id, T obj)
        {
            this.IdToObjectDictionary[id] = obj;
        }

        public void Delete(string id)
        {
            this.IdToObjectDictionary.Remove(id);
        }

        public List<T> GetAll()
        {
            return this.IdToObjectDictionary.Values.ToList();
        }

        public void ClearAll()
        {
            this.IdToObjectDictionary.Clear();
        }
    }
}
